﻿using Microsoft.Practices.Unity;
using ModernUILogViewer.Business;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;

namespace ModernUILogViewer.ViewModel
{
    public class ExternalEditorViewModel : BaseViewModel
    {
        #region Properties

        private string exePath;

        public string ExePath
        {
            get { return exePath; }
            set
            {
                exePath = value;
                RaisePropertyChanged(() => ExePath);
            }
        }

        private string commandArgs;

        public string CommandArgs
        {
            get { return commandArgs; }
            set
            {
                commandArgs = value;
                RaisePropertyChanged(() => CommandArgs);
            }
        }


        private readonly IDictionary<char, string> argumentList = new Dictionary<char, string>
        {
            {'f', "%f - FILE"},            
            {'d', "%d - DATE"},
            {'m', "%m - MESSAGE"},
            {'n', "%n - LINE"},
            {'l', "%l - LEVEL"},                        
        };

        public IDictionary<char, string> ArgumentList
        {
            get { return argumentList; }
        }
        
        #endregion

        #region Methods

        void OpenFileDialogExecute()
        {
            IUnityContainer container = UnityContainerProvider.Instance;
            IDialogService service = container.Resolve<IDialogService>(DialogServiceNS.DLG_OPEN_EXE_FILE);

            string[] files = service.OpenFile(false);

            if (files.Length > 0)
            {
                ExePath = files[0];
                //CommandArgs = commandArgs + ' ';
            }
        }

        void AddArgumentToCommandExecute(char param)
        {
            StringBuilder sb = new StringBuilder(commandArgs);
            sb.Append('%');
            sb.Append(param);

            CommandArgs = sb.ToString();
        }

        void ClearExecute()
        {
            ExePath = null;
            CommandArgs = null;
        }
        
        #endregion

        #region Commands

        public ICommand OpenFileDialog { get { return new MvvmFoundation.Wpf.RelayCommand(OpenFileDialogExecute); } }
        public ICommand AddArgumentToCommand { get { return new MvvmFoundation.Wpf.RelayCommand<char>(AddArgumentToCommandExecute); } }
        public ICommand Clear { get { return new MvvmFoundation.Wpf.RelayCommand(ClearExecute); } }
        
        #endregion

    }
}
